---
title: Workflow Cancellation
sidebarTitle: Cancelling a Workflow
description: How to cancel workflows
---

Workflows can be cancelled during execution to stop processing immediately and free up resources. This is particularly useful for long-running workflows, background tasks, or when user requirements change mid-execution.

The cancellation system provides graceful termination with proper cleanup and event logging.

### When to Use Cancellation

- **User-initiated stops**: Allow users to cancel long-running processes
- **Resource management**: Free up computational resources when workflows are no longer needed
- **Priority changes**: Cancel lower-priority workflows to make room for urgent tasks

### Cancelling Background Workflows

For workflows running in the background (using `background=True`), you can cancel them using the `run_id`:

## Example

```python
import asyncio
from agno.workflow import Workflow
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.workflow.step import Step

# Setup workflow
long_running_workflow = Workflow(
    name="Long Running Analysis",
    steps=[
        Step(name="Research", agent=Agent(model=OpenAIChat(id="gpt-5-mini"), instructions="You are a helpful assistant that can research the web.")),
        Step(name="Deep Analysis", agent=Agent(model=OpenAIChat(id="gpt-5-mini"), instructions="You are a helpful assistant that can analyze the web.")),
        Step(name="Report Generation", agent=Agent(model=OpenAIChat(id="gpt-5-mini"), instructions="You are a helpful assistant that can generate a report.")),
    ]
)

async def main():
    # Start background workflow
    bg_response = await long_running_workflow.arun(
        input="Comprehensive market analysis for emerging technologies",
        background=True
    )
    
    print(f"Started workflow with run_id: {bg_response.run_id}")
    
    # Simulate some time passing
    await asyncio.sleep(5)
    
    # Cancel the workflow
    cancellation_result = long_running_workflow.cancel_run(bg_response.run_id)
    
    if cancellation_result:  # cancellation_result is a bool
        print(f"✅ Workflow {bg_response.run_id} cancelled successfully")
    else:
        print(f"❌ Failed to cancel workflow {bg_response.run_id}")

asyncio.run(main())
```

<Note>
When a workflow in streaming mode is cancelled, a specific event is triggered: `WorkflowRunEvent.workflow_cancelled` or simply known as `WorkflowCancelledEvent`
</Note>

## Developer Resources

- [Workflow Cancellation](/examples/concepts/workflows/06_workflows_advanced_concepts/workflow_cancellation)   